﻿package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;

import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;

import com.alipay.sdk.util.LogUtils;
import com.amap.api.location.AMapLocation;
import com.badibadi.infos.DisanFangModel;
import com.badibadi.infos.IsOAuthLoginModel;
import com.badibadi.infos.LoginModel;
import com.badibadi.infos.Record_Info_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.AMapLocationUtils;
import com.badibadi.mytools.AMapLocationUtils.getAMapLocation;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.StringUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.socket.Send_Geshi_Tool;
import com.badibadi.uniclubber.R;
//import com.lidroid.xutils.util.LogUtils;
import com.view.my_view.ValidateImageView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

//forgot_password Login MyShare  l_x AfterLandingActivity LogUtils login_btn_QQ
public class LoginActivity extends BaseActivity implements Callback,
		OnClickListener, PlatformActionListener {

	private static String TAG = "LoginAcitivty";
	private static final int MSG_USERID_FOUND = 1;
	private static final int MSG_LOGIN = 2;
	private static final int MSG_AUTH_CANCEL = 3;
	private static final int MSG_AUTH_ERROR = 4;
	private static final int MSG_AUTH_COMPLETE = 5;
	private TextView login_registration;
	private AutoCompleteTextView login_edit_username, login_edit_passward;// 用户名，密码
	private TextView forgot_password;

	private Button btn_login, btn_sina, btn_qq, btn_facebook;
	private Map<String, Object> UserData;
	private static final String SaveUserName = "user";
	private String USER_NAME_KEY = "UserName";
	private String PASS_WORD_KEY = "PassWord";
	private static final String FIRST_RUN = "first";
	private Intent intent = new Intent();
	private SharedPreferences preferences;

	private LoginModel loginModel;
	AMapLocationUtils aMapLocationUtils;
	/**
	 * 统计登录次数
	 */
	private int loginCount = 0;
	Dialog dialog;
	// 随机数（用于验证码）
	private String[] number = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
			"0" };
	private String yanzhengma;// 保存验证码
	private ValidateImageView validateImageView;// 验证码图

	private String map_x, map_y;// 开始的时候定位

	// public Vector<String> msgs;
	// NetConnect netConnect;
	// MyActivity myactivity;

	DisanFangModel disanFangModel;
	IsOAuthLoginModel isOAuthLoginModel;// 验证是否
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				// if (Send_Geshi_Tool.socketThread != null) {
				// Send_Geshi_Tool.socketThread.Send(Send_Geshi_Tool
				// .fengzhuang_Login(Utils.getUid(LoginActivity.this),
				// Utils.getPwd(LoginActivity.this)));
				// } else {
				// Send_Geshi_Tool.startSocket(mhandler, mhandlerSend,
				// error_handler, LoginActivity.this);
				// handler.sendEmptyMessageDelayed(5, 1000);
				// }

				Utils.ExitPrgress(LoginActivity.this);
				Intent intent = new Intent(LoginActivity.this,
						AfterLandingActivity.class);
				intent.putExtra("isStartSocket", true);
				intent.putExtra("isStartTwoClose", true);
				startActivity(intent);
				LoginActivity.this.finish();

				try {
					Constants.Dengchu_handler.sendEmptyMessage(4);
					Constants.Before_handler.sendEmptyMessage(1);
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 2:
				Utils.ExitPrgress(LoginActivity.this);
				try {
					Utils.showMessage(LoginActivity.this, getResources()
							.getString(R.string.NetworkConnectionFailure));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				Utils.ExitPrgress(LoginActivity.this);
				Utils.showMessage(LoginActivity.this,
						getResources().getString(R.string.l_net_error));
				loginCount++;
				break;
			case 4:
				Utils.ExitPrgress(LoginActivity.this);
				LoginActivity.this.finish();
				break;
			case 5:
				if (Send_Geshi_Tool.socketThread != null) {
					Send_Geshi_Tool.socketThread.Send(Send_Geshi_Tool
							.fengzhuang_Login(Utils.getUid(LoginActivity.this),
									Utils.getPwd(LoginActivity.this)));
				}
				break;

			case 6:// 第三方登录验证是否切换到填写邮箱等等资料的界面
				try {
					isOAuthLogin(disanFangModel.getId());
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 7:// //判断是否进入到填写第三方资料界面
				try {
					// isOAuthLogin(disanFangModel.getId());
					if (isOAuthLoginModel.getOAuthUser() != null) {
						if (isOAuthLoginModel.getOAuthLogin().equals("1")
								&& isOAuthLoginModel.getOAuthUser().getEmail() == null
								|| isOAuthLoginModel.getOAuthUser().getEmail()
										.equals("0")
								&& isOAuthLoginModel.getOAuthUser()
										.getArea_id() == null
								|| isOAuthLoginModel.getOAuthUser()
										.getArea_id().equals("0")) {

							Intent intent2 = new Intent(LoginActivity.this,
									TheThirdPartyActivity.class);
							intent2.putExtra("uid", disanFangModel.getId());
							intent2.putExtra("Pwd", disanFangModel.getPass());
							 MySharePreferences preferences = new
							 MySharePreferences(
							 LoginActivity.this, SaveUserName);
//							  preferences.put(USER_NAME_KEY, username);
//							  preferences.put(PASS_WORD_KEY, password);
							 preferences.put("uid", disanFangModel.getId());
							 preferences.put("Pwd", disanFangModel.getPass());
							startActivity(intent2);

						} else {
							Intent intent3 = new Intent(LoginActivity.this,
									AfterLandingActivity.class);
//							LoginActivity.this.startActivity(intent3);
							MySharePreferences preferences = new MySharePreferences(
									LoginActivity.this, SaveUserName);
							// preferences.put(USER_NAME_KEY, username);
							// preferences.put(PASS_WORD_KEY, password);
							preferences.put("uid", disanFangModel.getId());
							preferences.put("Pwd", disanFangModel.getPass());
							intent3.putExtra("isStartSocket", true);
							intent3.putExtra("isStartTwoClose", true);
							// exit();
							startActivity(intent3);

						}
					}

				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 8:
				Utils.ExitPrgress(LoginActivity.this);
				try {
					Utils.showMessage(LoginActivity.this, getResources()
							.getString(R.string.TheAccountOrPasswordError));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			// TheAccountOrPasswordError
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ShareSDK.initSDK(LoginActivity.this);
		setFullScreen();
		Location();// 定位
		setContentView(R.layout.login);
		// Intent intent=new
		// Intent(LoginActivity.this,TheThirdPartyActivity.class);
		// startActivity(intent);
		init();

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Constants.Dengchu_handler = handler;
	}

	/**
	 * 定位
	 */
	private void Location() {

		// 这只是一个使用接口的模板
		final AMapLocationUtils aMapLocationUtils = new AMapLocationUtils(
				LoginActivity.this);
		new Thread(new Runnable() {
			boolean isRun = true;

			@Override
			public void run() {
				// TODO Auto-generated method stub

				while (isRun) {
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						   
					}
					aMapLocationUtils
							.setOnAMapLocationClistener(new getAMapLocation() {

								@Override
								public void getAMapLocation(
										AMapLocation aMapLocation) {
									// TODO Auto-generated method stub
									//    
									if (aMapLocation != null) {
										//    
										// "经度-》"
										// + aMapLocation
										// .getLongitude()
										// + "纬度=》"
										// + aMapLocation
										// .getLatitude());
										map_x = "" + aMapLocation.getLatitude();
										map_y = ""
												+ aMapLocation.getLongitude();
										isRun = false;
									}
								}
							});
				}

			}
		}).start();
	}

	private void init() {
		// TODO Auto-generated method stub
		dialog = new Dialog();
		loginModel = new LoginModel();

		MySharePreferences preferences = new MySharePreferences(
				LoginActivity.this, SaveUserName);

		login_edit_username = (AutoCompleteTextView) findViewById(R.id.login_edit_username);
		// login_edit_username.setText("man123");
		// login_edit_username.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		// //删除线
		login_edit_passward = (AutoCompleteTextView) findViewById(R.id.login_edit_passward);
		// login_edit_passward.setText("888888");
		// // SaveUserName
		//    
		// if(preferences.isContains(USER_NAME_KEY) &&
		// preferences.isContains(PASS_WORD_KEY))
		// {
		login_edit_username.setText(preferences.get(USER_NAME_KEY, ""));
		login_edit_passward.setText(preferences.get(PASS_WORD_KEY, ""));
		// }
		// login_edit_username.setText("cccccc");
		// login_edit_passward.setText("admin");
		// .getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //删除线
		login_registration = (TextView) findViewById(R.id.login_registration);
		login_registration.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); // 添加线

		// login_edit_passward.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		// //删除线
		forgot_password = (TextView) findViewById(R.id.forgot_password);
		forgot_password.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); // 添加线
		btn_sina = (Button) this.findViewById(R.id.login_btn_Sina);
		btn_qq = (Button) this.findViewById(R.id.login_btn_QQ);

		btn_facebook = (Button) this.findViewById(R.id.login_btn_facebook);

		btn_login = (Button) this.findViewById(R.id.btn_login);
		btn_login.setOnClickListener(LoginActivity.this);
		forgot_password.setOnClickListener(LoginActivity.this);
		btn_sina.setOnClickListener(LoginActivity.this);
		btn_qq.setOnClickListener(LoginActivity.this);
		btn_facebook.setOnClickListener(LoginActivity.this);

		login_registration.setOnClickListener(this);
		UserData = new HashMap<String, Object>();
	}

	@Override
	// 单机登录
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.login_btn_Sina:
			authorize(new SinaWeibo(LoginActivity.this));
//			   
			break;
		case R.id.login_btn_QQ:
			authorize(new QZone(LoginActivity.this));
//			   
			// Intent intent=new
			// Intent(LoginActivity.this,TheThirdPartyActivity.class);
			// intent.putExtra("uid", "181");
			// intent.putExtra("Pwd", "NzQ");
			// startActivity(intent);
			break;
		// case R.id.login_btn_google_add:
		// authorize(new LinkedIn(LoginActivity.this));
		// break;
		case R.id.login_btn_facebook:
			authorize(new Facebook(LoginActivity.this));
			break;
		case R.id.forgot_password:
			intent.setClass(this, ForgotPassWordActivity.class);
			startActivity(intent);
			break;
		case R.id.login_registration:
			intent.setClass(this, RegirstrationActivity.class);
			startActivity(intent);
			finish();
			break;
		case R.id.btn_login:
			if (loginCount >= 3) {
				// 输入验证码
				LayoutInflater inflater = LayoutInflater
						.from(LoginActivity.this);
				View view = inflater.inflate(R.layout.dialog_yanzhengma, null);
				validateImageView = (ValidateImageView) view
						.findViewById(R.id.validateImageView1);
				gengxin();
				TextView huantu = (TextView) view.findViewById(R.id.huantu);
				// 换图
				huantu.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						gengxin();
					}
				});
				dialog.CreateDialog(LoginActivity.this, ""
						+ getResources().getString(R.string.l_x1), null, view);
				dialog.setOnDialogClistener(new Dialog.ViewDialogClistener() {

					@Override
					public void ret(View view) {
					}

					@Override
					public void ok(View view) {

						EditText et_shuru = (EditText) view
								.findViewById(R.id.et_shuru);
						if (startValidata(et_shuru) == true) {
							startLogin();
						} else {

						}

					}
				});
			} else {// 登录
				startLogin();
			}

			break;
		}
	}

	/**
	 * 登录（非第三方）
	 */
	public void startLogin() {
//		Log
		// 开始登录
		if (!StringUtils.isEmpty(login_edit_username.getText().toString()
				.trim())
				&& !StringUtils.isEmpty(login_edit_passward.getText()
						.toString().trim())) {
			Login(login_edit_username.getText().toString().trim(),
					login_edit_passward.getText().toString().trim());
		} else {
			Toast.makeText(
					getApplicationContext(),
					""
							+ getResources().getString(
									R.string.l_plese_enterthe_data), 0).show();
		}
	}

	// /**
	// * 非第三方登录
	// */
	// private void Login() {
	// // TODO Auto-generated method stub
	// String UserName = login_edit_username.getText().toString().trim();
	// String PassWord = login_edit_passward.getText().toString().trim();
	// 在这里将输入的用户名和密码提交上去，判断是否成功
	// if (true) {
	// MySharePreferences preferences = new MySharePreferences(
	// LoginActivity.this, SaveUserName);
	// preferences.put(USER_NAME_KEY, UserName);
	// preferences.put(PASS_WORD_KEY, PassWord);
	// //    
	// //    
	// intent.setClass(this, AfterLandingActivity.class);
	// if (intent != null)
	// startActivity(intent);
	// finish();
	// }
	// d登录
	private void Login(final String username, final String password) {
		Utils.showPrgress(LoginActivity.this);
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("name", username);
				map.put("password", password);
				map.put("map_x", map_x);
				map.put("map_y", map_y);
				String content = Utils.sendRequest(map, Constants.BadiUrl
						+ "login");
				if (content != null) {
					Results results = Utils.checkResult_NNN(
							getApplicationContext(), content);
					if (results != null) {
//						    
						//    ("登陆返回的结果是:" + content);
						if (results.isRet()) {
							try {
								loginModel = JSONUtils.getEntityByJsonString(// Utils.JSONTokener(
										results.getRetmsg(), LoginModel.class);
								MySharePreferences preferences = new MySharePreferences(
										LoginActivity.this, SaveUserName);
								preferences.put(USER_NAME_KEY, username);
								preferences.put(PASS_WORD_KEY, password);
								preferences.put("uid", loginModel.getId());
								preferences.put("Pwd", loginModel.getPass());
								//    
								Constants.model = loginModel;
								handler.sendEmptyMessage(1);
							} catch (JSONException e) {
								//    ("解析出错");
								   
							} catch (Exception e) {
								// TODO Auto-generated catch block
								   
							}
						} else {
							handler.sendEmptyMessage(8);
						}
					} else {
						// ShowToast(getApplicationContext(), "登录失败");
						handler.sendEmptyMessage(3);
					}

				} else {
					handler.sendEmptyMessage(2);
				}
			}

		}).start();

		// 在这里将输入的用户名和密码提交上去，判断是否成功
		// if (true) {
		// MySharePreferences preferences = new MySharePreferences(
		// LoginActivity.this, SaveUserName);
		// preferences.put(USER_NAME_KEY, UserName);
		// preferences.put(PASS_WORD_KEY, PassWord);
		// //    
		// //    
		// intent.setClass(this, AfterLandingActivity.class);
		// if (intent != null)
		// startActivity(intent);
		// finish();
		// }
	}

	// private void set_socket(String mes) {
	// try {
	// myactivity = new MyActivity(this);
	// msgs = new Vector<String>();
	// netConnect = new NetConnect(this, mes, true);
	// netConnect.start();
	// } catch (Exception e) {
	//    ("报错原因:" + e.getMessage());
	// }
	// }

	private void authorize(Platform plat) {
		if (plat == null) {
			return;
		}
		if (plat.isValid()) {
			String userId = plat.getDb().getUserId();// 如果已经授权，就从数据库中取出用户的id
			//    
			if (userId != null) {
				UIHandler.sendEmptyMessage(MSG_USERID_FOUND, this);
				login(plat.getName(), userId, null);
				//    
				return;
			}
		}
		plat.setPlatformActionListener(this);
		// true不适用sso授权，false使用sso授权
		plat.SSOSetting(false);
		plat.showUser(null);
	}

	@Override
	// 跳转
	public boolean handleMessage(Message msg) {
		// TODO Auto-generated method stub
		switch (msg.what) {
		case MSG_USERID_FOUND: {// 存在
			Toast.makeText(this, R.string.userid_found, Toast.LENGTH_SHORT)
					.show();
		}
			break;
		case MSG_LOGIN: {// 授权后登录
			String text = getString(R.string.logining, msg.obj);
			Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
			if (msg.obj.equals("QZone")) {
				Platform p = new QZone(LoginActivity.this);
				//    
				disanfangLogin("qq", p.getDb().getUserId(), p.getDb()
						.getToken(), p.getDb().getUserName());
			} else if (msg.obj.equals("SinaWeibo")) {
				Platform p = new SinaWeibo(LoginActivity.this);
				disanfangLogin("sina", p.getDb().getUserId(), p.getDb()
						.getToken(), p.getDb().getUserName());
			} else if (msg.obj.equals("Facebook")) {
				Platform p = new Facebook(LoginActivity.this);
				disanfangLogin("facebook", p.getDb().getUserId(), p.getDb()
						.getToken(), p.getDb().getUserName());
			}
			// get User Gender 获取用户的性别
			// get Token Secret 获得令牌的秘密
			// get Token 获得令牌
			// get Platform Nname 让平台Nname
			//
			// get Platform Version 获得平台版本
			// get Expires Time 有到期的时间

			// "992A7FCFC78A05E78F4897244B21DE73"
			// disanfangLogin("qq", p.getDb().getUserId(),
			// p.getDb().getPlatformVersion(), text);

			// 如果执行到了这一步,说明授权是成功的userId是存在的,然后可以直接跳转到登录后;
			// intent.setClass(LoginActivity.this, TheThirdPartyActivity.class);
			// startActivity(intent);

			// Builder builder = new Builder(this);
			// builder.setTitle(R.string.if_register_needed);
			// builder.setMessage(R.string.after_auth);
			// builder.setPositiveButton(R.string.ok, null);
			// builder.create().show();
		}
			break;
		case MSG_AUTH_CANCEL: {
			Toast.makeText(this, R.string.auth_cancel, Toast.LENGTH_SHORT)
					.show();
		}
			break;
		case MSG_AUTH_ERROR: {
			// Toast.makeText(this, R.string.auth_error, Toast.LENGTH_SHORT)
			// .show();
			// LogUtils.d("错误622");
		}
			break;
		case MSG_AUTH_COMPLETE: {
			Toast.makeText(this, R.string.auth_complete, Toast.LENGTH_SHORT)
					.show();

		}
			break;
		}
		return false;
	}

	private void login(String plat, String userId,
			HashMap<String, Object> userInfo) {
		Message msg = new Message();
		msg.what = MSG_LOGIN;
		msg.obj = plat;
		UIHandler.sendMessage(msg, this);
	}

	@Override
	// 取消
	public void onCancel(Platform arg0, int action) {
		// TODO Auto-generated method stub
		if (action == Platform.ACTION_USER_INFOR) {
			UIHandler.sendEmptyMessage(MSG_AUTH_CANCEL, this);
		}
	}

	@Override
	// 没有授权成功返回参数
	public void onComplete(Platform platform, int action,
			HashMap<String, Object> res) {
		// TODO Auto-generated method stub
		//    
		// LogUtils.d("" + platform.getName());
		if (action == Platform.ACTION_USER_INFOR) {
			UIHandler.sendEmptyMessage(MSG_AUTH_COMPLETE, this);
			// login(platform.getName(), platform.getDb().getUserId(), res);
			UserData = res;
			// LogUtils.d("" + platform.getName());

			if (platform.getName().equals("QZone")) {
				// QZone qz = new QZone(LoginActivity.this);
				// LogUtils.d("" + platform.isValid());
				if (platform.isValid()) {

					disanfangLogin("qq", platform.getDb().getUserId(), platform
							.getDb().getToken(), platform.getDb().getUserName());
				}

			} else if (platform.getName().equals("SinaWeibo")) {
				// SinaWeibo s = new SinaWeibo(LoginActivity.this);
				if (platform.isValid())
					disanfangLogin("sina", platform.getDb().getUserId(),
							platform.getDb().getToken(), platform.getDb()
									.getUserName());
			} else if (platform.getName().equals("Facebook")) {
				// Facebook fa = new Facebook(LoginActivity.this);
				// LogUtils.d("facebook是否授权" + platform.isValid());

				if (platform.isValid())
					disanfangLogin("facebook", platform.getDb().getUserId(),
							platform.getDb().getToken(), platform.getDb()
									.getUserName());

			}

		}
	}

	/**
	 * APP第三方登录接口--首先验证该用户是否使用过----POST
	 * 
	 * @param $type
	 *            第三方登录方式 qq ：QQ登录 sina:新浪登录 facebook :facebook登录
	 * @param $openId
	 *            //用户身份的唯一标示符
	 * @param $accessToken
	 *            //当前用户在此网站中使用应用登录的授权信息
	 * @param $nickName
	 *            第三方的使用昵称
	 * @param $map_x
	 *            //纬度
	 * @param $map_y
	 *            //经度 handl
	 */
	private void disanfangLogin(final String type, final String openId,
			final String accessToken, final String nickName) {
		// LogUtils.d("授权已经成功了，现在开始进入第三方登录");
		// isOAuthLogin
		// Utils.showPrgress(LoginActivity.this);
		// LogUtils.d("授权已经成功了，现在开始进入第三方登录11");
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				// LogUtils.d("进入现场");
				Map<String, Object> map = new HashMap<String, Object>();
				// LogUtils.d("封装map");
				map.put("type", type);
				// LogUtils.d("封装type");
				map.put("openId", openId);
				// LogUtils.d("封装openId");
				map.put("accessToken", accessToken);
				// LogUtils.d("封装accessToken");
				map.put("nickName", nickName);
				// LogUtils.d("封装nickName");
				map.put("map_x", map_x);
				// LogUtils.d("封装map_x");
				map.put("map_y", map_y);
				// LogUtils.d("封装map_y");
				// LogUtils.d("授权已经成功了，现在开始进入第三方登录2");
				   
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "OAuth/login");
//				 LogUtils.d("授权已经成功了，现在开始进入第三方登录3==>"+map.toString());
				// String content = Utils.getRequest(Constants.BadiUrl
				// + "record_info?rid=" +
				// rid+"&uid="+Utils.getUid(getActivity()));
				//    (Constants.BadiUrl + "friends?uid=" + id
				// + "&page=" + page + "&pageNum=" + 10);
				//    
				if (content != null) {
					Results results = Utils.checkResult_NNN(LoginActivity.this,
							content);
					// LogUtils.d("授权已经成功了，现在开始进入第三方登录4");
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
//						   
						try {
							disanFangModel = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), DisanFangModel.class);
							// LogUtils.d("开始资料转移...");
							if (disanFangModel != null) {// 这里的操作仅仅只是为了判断是否进入资料
								LoginModel loginModel = new LoginModel();
								// LogUtils.d("开始资料转移...1");
								loginModel.setAmount(disanFangModel.getAmount());
								// LogUtils.d("开始资料转移...2");
								loginModel.setId(disanFangModel.getId());
								// LogUtils.d("开始资料转移...3");
								loginModel.setPass(disanFangModel.getPass());
								// LogUtils.d("开始资料转移...4");
								loginModel.setIs_login(disanFangModel
										.getIs_login());
								// LogUtils.d("开始资料转移...5");
								Constants.model = loginModel;
								// LogUtils.d("第三方登录验证用户是否使用过结束了");
							}
							handler.sendEmptyMessage(6);
						} catch (Exception e) {
							   
						}
					} else {
						handler.sendEmptyMessage(3);
						// LogUtils.d("授权失败了");
					}
				} else {
					handler.sendEmptyMessage(1);
					// LogUtils.d("授权失败了");
				}
			}
		});

	}

	/*
	 * 获取登录用户是否经过第三方登录，并且是否资料（邮箱、性别和地区）没完善，没有则需显示弹框供用户修改-----POST
	 * 
	 * @param $uid 登录用户ID----isOAuthLoginModel
	 */
	private void isOAuthLogin(final String uid) {
		// Utils.showPrgress(LoginActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid);
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "OAuth/isOAuthLogin");
				if (content != null) {
					Results results = Utils.checkResult_NNN(LoginActivity.this,
							content);
//					   
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
//						   
						try {
							isOAuthLoginModel = JSONUtils
									.getEntityByJsonString(results.getRetmsg(),
											IsOAuthLoginModel.class);
							handler.sendEmptyMessage(7);

						} catch (Exception e) {
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		ShareSDK.stopSDK(this);
		super.onDestroy();

	}

	@Override
	public void onError(Platform arg0, int action, Throwable t) {
		// TODO Auto-generated method stub
		if (action == Platform.ACTION_USER_INFOR) {
			UIHandler.sendEmptyMessage(MSG_AUTH_ERROR, this);
			// LogUtils.i("834 action="+action+"Throwable="+t);/
		}
		   
	}

	/**
	 * 判断验证码是否一致
	 * 
	 * @param et
	 *            文本框
	 */
	public boolean startValidata(EditText et) {
		if (et.getText().toString().trim().equals(yanzhengma)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 更新验证码
	 */
	public void gengxin() {
		yanzhengma = getValidataText(validateImageView, number);
	}

	/**
	 * 获取验证码
	 * 
	 * @return
	 */
	public String getValidataText(ValidateImageView img, String[] num) {
		StringBuffer buff = new StringBuffer("");
		String[] c = img.getValidataAndSetImage(num);
		for (int i = 0; i < c.length; i++) {
			buff.append(c[i]);
		}
		String yzm = buff.substring(0, buff.length());
		return yzm;
	}
}
